package com.gx.smart.building.ui.warning

import android.content.Intent
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.TimeUtils
import com.gx.smart.base.BaseMVVMActivityV3
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivityWarningDetailBinding
import com.gx.smart.building.ui.common.adapter.ImageAdapter
import com.gx.smart.building.ui.warning.event.WarningMessageEvent
import com.gx.smart.building.ui.warning.viewmodel.WarningViewModel
import com.gx.smart.lib.http.api.response.MessageAlarm
import org.greenrobot.eventbus.EventBus

class WarningDetailActivity : BaseMVVMActivityV3<WarningViewModel>() {

    private lateinit var viewBinding: ActivityWarningDetailBinding
    private lateinit var adapter: ImageAdapter
    private lateinit var detail: MessageAlarm
    private lateinit var id: String
    override fun onBindViewModel(): Class<WarningViewModel> = WarningViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_warning_detail

    override fun getToolbarTitle(): String = "消息详情"

    override fun initContentView(root: ViewGroup) {
        viewBinding = ActivityWarningDetailBinding.inflate(layoutInflater, root, true)
        adapter = ImageAdapter(this)
        viewBinding.recyclerView.adapter = adapter
        viewBinding.transform.setOnClickListener {
            val intent = Intent(this, FaultReportActivity::class.java)
            intent.putExtra("dealType", 1)
            startActivity(intent)
            EventBus.getDefault().postSticky(WarningMessageEvent(detail))
        }
        viewBinding.process1.setOnClickListener {
            val intent = Intent(this, WarningSubmitActivity::class.java)
            intent.putExtra("dealType", 2)
            startActivity(intent)
            EventBus.getDefault().postSticky(WarningMessageEvent(detail))
        }
        viewBinding.process2.setOnClickListener {
            val intent = Intent(this, WarningSubmitActivity::class.java)
            intent.putExtra("dealType", 3)
            startActivity(intent)
            EventBus.getDefault().postSticky(WarningMessageEvent(detail))

        }
        viewBinding.process3.setOnClickListener {
            val intent = Intent(this, WarningSubmitActivity::class.java)
            intent.putExtra("dealType", 4)
            startActivity(intent)
            EventBus.getDefault().postSticky(WarningMessageEvent(detail))
        }
    }

    override fun initData() {
        id = intent.getStringExtra("id")!!
    }

    private fun doAction() {
        viewModel.detail(id!!).observe(this) {
            detail = it
            viewBinding.alarmLevelName.text = it.alarmLevelName
            val alarmTime = TimeUtils.millis2String(it.alarmTime, "yyyy-MM-dd HH:mm:ss")
            viewBinding.alarmTime.text = alarmTime
            viewBinding.alarmTypeName.text = it.alarmTypeName
            viewBinding.subSystemName.text = it.subSystemName
            viewBinding.alarmResName.text = it.alarmResName
            viewBinding.areaName.text = it.areaName
            viewBinding.alarmDetail.text = it.alarmDetail
//            adapter.refresh(it.messageAlarmFileDtoList.map { item ->
//                BuildConfig.FILE_UPLOAD_URL + item.fileUrl
//            })
            //1-未处理，2-已处理
            viewBinding.alarmResolveState.text = if (it.alarmResolveState == 1) {
                viewBinding.submitLayout.visibility = View.VISIBLE
                "未处理"
            } else {
                viewBinding.submitLayout.visibility = View.GONE
                "已处理"
            }
            viewBinding.dealType.text = when (it.dealType) {
                1 -> "转工单"
                2 -> "误报消除"
                3 -> "已解决"
                4 -> "忽略"
                else -> ""
            }
            viewBinding.dealUserName.text = it.dealUserName
            viewBinding.dealDesc.text = it.dealDesc
            viewBinding.dealWorkOrder.text = it.dealWorkOrder
        }

    }

    override fun onStart() {
        super.onStart()
        doAction()
    }
}